// VBConversions Note: VB project level imports
using System.Collections.Generic;
using System;
using System.Linq;
using System.Drawing;
using System.Diagnostics;
using Microsoft.VisualBasic;
using System.Xml.Linq;
using Microsoft.VisualBasic.Compatibility;
using System.Windows.Forms;
using System.Collections;
using System.Data;
// End of VB project level imports


namespace 喷码数据生成工具
{
	public partial class frm_SearchReplaceWindow
	{
		frm_TextEdit frm;
		int SearchIndex;
		private const int MaxHeight = 337;
		private const int MinHeight = 152;
		private bool isLoadAscii = false;
		private bool DataLoading = false;
#region 构造函数
		public frm_SearchReplaceWindow(string sText)
		{
			
			// 此调用是 Windows 窗体设计器所必需的。
			InitializeComponent();
			
			// 在 InitializeComponent() 调用之后添加任何初始化。
			TB_SearchValue.Text = sText;
		}
		public frm_SearchReplaceWindow()
		{
			
			// 此调用是 Windows 窗体设计器所必需的。
			InitializeComponent();
			
			// 在 InitializeComponent() 调用之后添加任何初始化。
			
		}
#endregion
public frm_TextEdit FatherForm
		{
			get
			{
				return frm;
			}
			set
			{
				frm = value;
			}
		}
		public void Btn_Cancel_Click(System.Object sender, System.EventArgs e)
		{
			this.Close();
		}
		
		public void Btn_Search_Click(System.Object sender, System.EventArgs e)
		{
			SearchSub();
		}
		private void SearchSub()
		{
			string SearchValue = CommFunction.TranslateText(TB_SearchValue.Text);
			System.Windows.Forms.RichTextBoxFinds SearchOption = new System.Windows.Forms.RichTextBoxFinds();
			if (CB_UpowerdLower.Checked)
			{
				SearchOption = RichTextBoxFinds.MatchCase;
			}
			else
			{
				SearchOption = RichTextBoxFinds.None;
			}
			frm_TextEdit with_1 = frm;
			
			if (SearchIndex < 0)
			{
				SearchIndex = 0;
			}
			SearchIndex = with_1.TextBox.Find(SearchValue, SearchIndex, with_1.TextBox.TextLength, SearchOption);
			if (SearchIndex > 0)
			{
				with_1.TextBox.SelectionStart = SearchIndex;
				with_1.TextBox.SelectionLength = SearchValue.Length;
				SearchIndex = SearchIndex + SearchValue.Length;
			}
			else
			{
				MessageBox.Show(this, "已搜索至文档结束！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
			}
		}
		private void LoadAsciiValue()
		{
			if (isLoadAscii)
			{
				this.Height = MinHeight;
				isLoadAscii = false;
			}
			else
			{
				this.Height = MaxHeight;
				isLoadAscii = true;
			}
			if (DataLoading == false)
			{
				//加载数据
				int sCount = 0;
				ListViewItem lItem = null;
				for (var i = 1; i <= 255; i++)
				{
					if (sCount == 0)
					{
						lItem = LV_Ascii.Items.Add(System.Convert.ToString(i));
						lItem.SubItems.Add(CommFunction.ValueToChar(System.Convert.ToInt32(i)));
						sCount++;
					}
					else if (sCount == 1)
					{
						if (lItem != null)
						{
							lItem.SubItems.Add(i);
							lItem.SubItems.Add(CommFunction.ValueToChar(System.Convert.ToInt32(i)));
						}
						sCount++;
					}
					else if (sCount == 2)
					{
						if (lItem != null)
						{
							lItem.SubItems.Add(System.Convert.ToString(i));
							lItem.SubItems.Add(CommFunction.ValueToChar(System.Convert.ToInt32(i)));
						}
						sCount = 0;
					}
				}
			}
		}
		
		public void frm_SearchReplaceWindow_Load(System.Object sender, System.EventArgs e)
		{
			this.Height = MinHeight;
		}
		
		public void Label3_Click(System.Object sender, System.EventArgs e)
		{
			LoadAsciiValue();
		}
		
		public void Btn_Replace_Click(System.Object sender, System.EventArgs e)
		{
			string SearchValue = CommFunction.TranslateText(TB_SearchValue.Text);
			string ReplaceValue = CommFunction.TranslateText(TB_ReplaceValue.Text);
			string Result = "";
			string tmp = frm.TextBox.SelectedText;
			System.Windows.Forms.RichTextBoxFinds SearchOption = new System.Windows.Forms.RichTextBoxFinds();
			
			frm_TextEdit with_1 = frm;
			if (CB_UpowerdLower.Checked)
			{
				if (tmp == SearchValue)
				{
					Result = frm.TextBox.Text.Substring(0, frm.TextBox.SelectionStart) + ReplaceValue + frm.TextBox.Text.Substring(frm.TextBox.SelectionStart + frm.TextBox.SelectedText.Length);
					frm.TextBox.Text = Result;
					return ;
				}
				SearchOption = RichTextBoxFinds.MatchCase;
			}
			else
			{
				if (tmp.ToLower() == SearchValue.ToLower())
				{
					Result = frm.TextBox.Text.Substring(0, frm.TextBox.SelectionStart) + ReplaceValue + frm.TextBox.Text.Substring(frm.TextBox.SelectionStart + frm.TextBox.SelectedText.Length);
					frm.TextBox.Text = Result;
					return ;
				}
				SearchOption = RichTextBoxFinds.None;
			}
			if (SearchIndex < 0 | SearchIndex > with_1.TextBox.TextLength)
			{
				SearchIndex = 0;
			}
			SearchIndex = with_1.TextBox.Find(SearchValue, SearchIndex, with_1.TextBox.TextLength, SearchOption);
			if (SearchIndex > 0)
			{
				with_1.TextBox.SelectionStart = SearchIndex;
				with_1.TextBox.SelectionLength = SearchValue.Length;
				SearchIndex = SearchIndex + SearchValue.Length;
				Result = frm.TextBox.Text.Substring(0, frm.TextBox.SelectionStart) + ReplaceValue + frm.TextBox.Text.Substring(frm.TextBox.SelectionStart + frm.TextBox.SelectedText.Length);
				frm.TextBox.Text = Result;
			}
			else
			{
				MessageBox.Show(this, "已搜索至文档结束！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
			}
			//  If Not String.IsNullOrEmpty(Result) Then frm.TextBox.Text = Result
		}
		
		public void Btn_ReplaceAll_Click(System.Object sender, System.EventArgs e)
		{
			try
			{
				string SearchValue = CommFunction.TranslateText(TB_SearchValue.Text);
				string ReplaceValue = CommFunction.TranslateText(TB_ReplaceValue.Text);
				string Result = frm.TextBox.Text;
				long Rcount = 0;
				long Rindex = 0;
				System.Windows.Forms.RichTextBoxFinds SearchOption = new System.Windows.Forms.RichTextBoxFinds();
				frm_TextEdit with_1 = frm;
				if (CB_UpowerdLower.Checked)
				{
					SearchOption = RichTextBoxFinds.MatchCase;
				}
				else
				{
					SearchOption = RichTextBoxFinds.None;
				}
				if (SearchIndex < 0 | SearchIndex >= with_1.TextBox.TextLength)
				{
					SearchIndex = 0;
				}
				while (!(SearchIndex < 0 | SearchIndex >= Result.Length))
				{
					SearchIndex = Search(Result, SearchIndex, SearchValue);
					if (SearchIndex >= 0)
					{
						Result = Result.Substring(0, SearchIndex) + ReplaceValue + Result.Substring(SearchIndex + SearchValue.Length);
						SearchIndex = SearchIndex + SearchValue.Length;
						Rcount++;
					}
					else
					{
						break;
					}
				}
				with_1.TextBox.Text = Result;
				if (Rcount > 0)
				{
					MessageBox.Show(this, "成功替换" + System.Convert.ToString(Rcount) + "处文本！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
				}
			}
			catch (Exception ex)
			{
				MessageBox.Show(this, ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
			}
		}
		public long Search(string OldText, int StartIndex, string SearchValue)
		{
			if (StartIndex < 0 | StartIndex >= OldText.Length)
			{
				SearchIndex = -1;
			}
			string TmpS = "";
			string sV = "";
			long Result = 0;
			if (CB_UpowerdLower.Checked)
			{
				TmpS = OldText.Substring(StartIndex);
				sV = SearchValue;
			}
			else
			{
				TmpS = OldText.ToLower().Substring(StartIndex);
				sV = SearchValue.ToLower();
			}
			Result = TmpS.IndexOf(sV);
			if (Result >= 0)
			{
				return System.Convert.ToInt64( Result + StartIndex);
			}
			else
			{
				return -1;
			}
		}
	}
}
